package com.example.ssedemo;

import com.example.ssedemo.bean.Message;
import com.example.ssedemo.service.SseEmitterManager;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.listener.MessageListener;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;

@Slf4j
@Component
public class InboxUserTopicListener implements MessageListener<Message> {
    @Override
    public void onMessage(CharSequence charSequence, Message message) {
        String id = message.getId();
        if (SseEmitterManager.EMITTERS.containsKey(id)) {
            SseEmitter sseEmitter = SseEmitterManager.EMITTERS.get(id);
            try {
                sseEmitter.send(message);
            } catch (IOException e) {
                log.info("用户不在线或者是不在这个客户端;{}", id);
            }
        }
    }
}
